_unit = (_this select 0);
//-------------------Night Vision-----------------------------//
if (currentVisionMode player == 1) then
{
	camUseNVG true;
} else {
	camUseNVG false;
	};
////////////////////////////////////////////////////////////////
//------------------------------------------------------------//
if (_unit == player) then {
  waituntil {alive player};
  player setPos getPos glbase;

  _squad = group player;
  _additionalSpawns = basesquad;
  lck_livingmates = [];
  {
    if (alive _x and _x!=player) then {lck_livingmates = lck_livingmates + [_x];};
  } foreach (units _squad)+(units _additionalSpawns);
  
  lck_actualmate = 0;
  _elozoactual=lck_actualmate;
  keyout = 0;
  //player globalchat format ["sq db=%1, act=%2", count lck_livingmates, name (lck_livingmates select lck_actualmate)];


  openMap false;
  0 fadesound 0;
  disableserialization;
  keyspressed = compile preprocessFile "Occupation\lackoRespawn_keydown.sqf";
  _displayID = (findDisplay 46) displaySetEventHandler ["KeyDown","_this call keyspressed"];

  _cam = "camera" camcreate [0,0,0];
  _cam cameraeffect ["internal", "back"] ;
  showCinemaBorder true;
  _cam camsettarget (lck_livingmates select lck_actualmate);
  _cam camsetrelpos [0,-5,2.5];
  _cam camcommit 0;

  _ido=0;
  while {_ido<=500} do {
    if (_ido mod 10 == 0) then {
[_unit] spawn {
_unit = (_this select 0);
while {player distance getpos glbase <5} do {
      titleText [format ["\n \n \n%1\nRight Arrow = next spawn\nLeft Arrow = previous spawn\nEnter = respawn", name(lck_livingmates select lck_actualmate)], "PLAIN DOWN",1];
	  sleep 1};
};
    };

    if (keyout > 0) exitwith {};

    _currentmate = lck_livingmates select lck_actualmate;
    _tempcnt=0;

    lck_livingmates = [];
    {
      if (alive _x and _x!=player and (_x distance glbase > 30)) then {
        lck_livingmates = lck_livingmates + [_x];
        if (_x==_currentmate) then {lck_actualmate = _tempcnt;};
        _tempcnt = _tempcnt + 1;
      };
    } foreach (units _squad)+(units _additionalSpawns);

    if (count lck_livingmates == 0) exitwith {};

    _cam camsettarget (lck_livingmates select lck_actualmate);
    _cam camsetrelpos [0,-5,2.5];
    _cam camcommit 0;    

    waituntil{camCommitted _cam};
    _ido = _ido + 0.02; 
    sleep 0.05;
  };

  (findDisplay 46) displayRemoveAllEventHandlers "KeyDown";

_newpos = getPos glbase;
_allowSpawn = true;


_groupSide =side (lck_livingmates select lck_actualmate);

{
if (_groupSide == CIVILIAN) then {} else {
private ["_groupSides"];
_groupSides = [WEST,EAST,INDEPENDENT];
_groupSides = _groupSides - [_groupSide];
	_near = (lck_livingmates select lck_actualmate) nearEntities 50;
	{
	//hint format ["%1",_groupSide];
	//hint format ["%1",(lck_livingmates select lck_actualmate)];
	if (_x countSide _near > 0) then {
	_allowSpawn = false;
	};
	}foreach _groupSides;
};
}foreach units _additionalSpawns;
if (_allowSpawn) then {
	if (keyout > 0) then {_newpos = getPos (lck_livingmates select lck_actualmate);if ((_newpos select 0)>50 and (_newpos select 1)>50) then {} else {_newpos = getPos glbase;};} else {_newpos = getPos glbase;};

_return = stance (lck_livingmates select lck_actualmate);
if (_return == "stand") then {_return = "amovpknlmstpsraswrfldnon";};
sleep 0.02;
if (_return == "crouch") then {_return = "amovpknlmstpsraswrfldnon";};
sleep 0.02;
if (_return == "prone") then {_return = "AmovPpneMstpSrasWrflDnon";};
player switchMove _return;
	camdestroy _cam;
	player cameraEffect ["terminate","back"];
  	0 fadesound 1;

	titleText [format ["Spawning on %1", name(lck_livingmates select lck_actualmate)], "BLACK in",2];
		_degrees = getDir (lck_livingmates select lck_actualmate) -180; 
		_spawnPos = [_newpos,  5, _degrees] call BIS_fnc_relPos; 
		player setDir _degrees +180;
		player setPos _spawnPos;
	IF (vehicle (lck_livingmates select lck_actualmate) != (lck_livingmates select lck_actualmate)) THEN {
		if ((vehicle (lck_livingmates select lck_actualmate)) emptyPositions "cargo"==0)then
				{
		camdestroy _cam;
		player cameraEffect ["terminate","back"];
		0 fadesound 1;
		_spawnPos = [_newpos, 15, 90] call BIS_fnc_relPos; 
		player setPos _spawnPos;
				titleText ["VEHICLE FULL", "BLACK",0.1];
		sleep 2;
		null = [player] execVM "Occupation\JIPspawn.sqf";
				}else{
			player moveincargo vehicle (lck_livingmates select lck_actualmate);
			camdestroy _cam;player cameraEffect ["terminate","back"];0 fadesound 1;
			titleText [format ["Spawned In %1's vehicle", name(lck_livingmates select lck_actualmate)], "BLACK in",2];
				};
					};
}  else {// SPAWN UNSUITABLE
		camdestroy _cam;
		player cameraEffect ["terminate","back"];
		0 fadesound 1;
		_spawnPos = [_newpos, 15, 90] call BIS_fnc_relPos; 
		player setPos _spawnPos;
				titleText ["Enemies Nearby. Cannot spawn.", "BLACK",0.1];
		sleep 2;
		null = [player] execVM "Occupation\JIPspawn.sqf";
	};// allowspawn
	};	
